/*
 * Created on 3 mai 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package maze;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @author deloor
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Maze {

    private ArrayList<Brick> _listBrick;
    private int _high;
    private int _length;
    private Random generator;
    private Set<Point> _brickCache;

    public Maze() {

        _listBrick = new ArrayList();
        generator = new Random();

        // petit Labyrinthe
        _high = 10;
        _length = 10;
        for (int x = 0; x < 10; x++) {
            _listBrick.add(new Brick(x, 0, Color.green, -1));
            _listBrick.add(new Brick(x, 10, Color.green, -1));
        }
        for (int y = 0; y <= 10; y++) {
            _listBrick.add(new Brick(0, y, Color.green, -1));
            _listBrick.add(new Brick(10, y, Color.green, -1));
        }


        for (int y = 3; y < 9; y++) {
            _listBrick.add(new Brick(8, y, Color.green, -1));
        }
        for (int y = 3; y < 8; y++) {
            _listBrick.add(new Brick(4, y, Color.green, -1));
        }

        _listBrick.add(new Brick(9, 6, Color.orange, 10));
        
        _brickCache = new HashSet<Point>();
        
        for (Brick b : _listBrick) {
            _brickCache.add(b.getPosition());
        }
    }

    public ArrayList<Brick> getListBrick() {
        return _listBrick;
    }

    public boolean isThereABrickHere(Point p) {
        return _brickCache.contains(p);
    }

    public Brick returnTheBrickHere(Point p)
    {
        for (Brick b : _listBrick) {
            if ((b.getPosition().equals(p))) {
                return (b);
            }
        }
        return null;
    }

    public Point findAPositionFreeRandomly() {
        Point p = new Point();
        do {
            p.setLocation(generator.nextInt(_length), generator.nextInt(_high));
        } while (isThereABrickHere(p) != false);
        return p;
    }

    public void rewardChange() {
        //retrait brick reward
        int nbRewardBrick = 0;
        for (int i = _listBrick.size() - 1; i >= 0; i--) {
            if (((Brick) (_listBrick.get(i))).getValue() == 10) {
                nbRewardBrick++;
                _listBrick.remove(i);
            }
        }
        for (int i = 0; i < nbRewardBrick; i++) {
            Point p = findAPositionFreeRandomly();
            _listBrick.add(new Brick((int) (p.getX()), (int) (p.getY()), Color.orange, 10));
        }

    }

    public int getHigh() {
        return this._high;
    }

    public int getLength() {
        return this._length;
    }
}
